# -*- coding: utf-8 -*-

'''eof
name:申请企业类型校验
code:R03_EG_EntTypeCheck
tableName:
columnName:
groups:企业业务信息校验
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime


def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


def convertCompanyType(type):
    if (u"港" in type) or (u"澳" in type) or (u"台" in type):
        return u"台企"

    if (u"外国(地区)"  in type) or (u"外国（地区）"  in type) :
        return u"外企"

    bool1 = (u"有限责任公司" in type and u"外商投资" in type)
    bool2 = (u"股份有限公司" in type and u"外商投资" in type)
    if (u"外" in type) and (bool1 or bool2) != True:
        return u"外投"

    if (u"分" in type) or (u"经营单位" in type) or (u"办事处" in type) or (u"办事机构" in type) or (u"机构" in type):
        return u"分支"

    if u"个人独资企业" in type:
        return u"个独"

    if u"个" in type:
        return u"个商"

    if u"股份有限公司" in type:
        return u"股份"

    if ((u"有限责任公司" in type) and (u"独资" in type)) or (u"一人有限责任公司" in type) or\
            (u"有限公司（自然人独资）" in type)or (u"有限公司(自然人独资)" in type):
        return u"一人"

    if u"有限责任公司" in type:
        return u"有限"

    if u"合伙" in type:
        return u"合伙"

    if (u"农民专业合作" in type) or (u"其他" in type):
        return u"其他"

    return u'缺失值'


def getRule03():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    basic_list = FormatData("basicList")
    if basic_list == [{}]:
        return u'缺失值'
    else:
        if len(basic_list) >= 1:
            for i in basic_list:
                try:
                    enterprise_type = i.get("enterpriseType")
                except:
                    return u'缺失值'
                result_type = convertCompanyType(enterprise_type)
                return result_type
            return u'缺失值'
        else:
            return u'缺失值'



if __name__ == "__main__":
    file_obj = open(r'../data/rating/R03_EG_EntTypeCheck/EG_DSJ.json', 'r')
    content = file_obj.read()
    EG_DSJ = json.loads(content, strict=False)
    aa = getRule03()
    print aa

result = getRule03()

